package org.krayne.swivel.container;

import java.awt.BorderLayout;
import java.awt.Component;

import javax.swing.JPanel;



/**
 * This simple container that packs the specified component towards the
 * specified direction.
 *  
 * @author dhsu
 */
public class PackContainer extends JPanel {
    private static final long serialVersionUID = -9004917519060832023L;
    
    /**
     * Constructs a container that packs the specified component towards the
     * specified direction.
     * 
     * @param component the component
     * @param packDirection the direction to pack towards
     */
    public PackContainer(Component component, PackDirection packDirection) {
        super(new BorderLayout());

        // for top, bottom, left, right, we can just use a border layout
        if(packDirection == PackDirection.TOP) {
            this.add(component, BorderLayout.NORTH);
            return;
        } else if(packDirection == PackDirection.LEFT) {
            this.add(component, BorderLayout.WEST);
            return;
        } else if(packDirection == PackDirection.RIGHT) {
            this.add(component, BorderLayout.EAST);
            return;
        } else if(packDirection == PackDirection.BOTTOM){
            this.add(component, BorderLayout.SOUTH);
            return;
        }

        // for the corners, we nest border layouts to pack twice
        JPanel leftRightPackContainer = new JPanel(new BorderLayout());
        if(packDirection == PackDirection.TOP_LEFT) {
            leftRightPackContainer.add(component, BorderLayout.WEST);
            this.add(leftRightPackContainer, BorderLayout.NORTH);
        } else if(packDirection == PackDirection.TOP_RIGHT) {
            leftRightPackContainer.add(component, BorderLayout.EAST);
            this.add(leftRightPackContainer, BorderLayout.NORTH);
        } else if(packDirection == PackDirection.BOTTOM_LEFT) {
            leftRightPackContainer.add(component, BorderLayout.WEST);
            this.add(leftRightPackContainer, BorderLayout.SOUTH);
        } else if(packDirection == PackDirection.BOTTOM_RIGHT) {
            leftRightPackContainer.add(component, BorderLayout.EAST);
            this.add(leftRightPackContainer, BorderLayout.SOUTH);
        }
    }
}
